﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLKDS.DTO;

namespace QLKDS.DAL
{
    public class StoreManager
    {
        public StoreInfo GetStoreById(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryStore = from d in db.Stores
                                    where d.Status.Equals(1)
                                    where d.Id.Equals(id)
                                    select new StoreInfo { Id = d.Id, Name = d.Name, Status = d.Status, Address = d.Address, TaxCode = d.TaxCode, Mobile = d.Mobile, Phone = d.Phone,  Description = d.Description, CreatedBy = d.CreatedBy, CreatedDate = d.CreatedDate, ModifiedBy = d.ModifiedBy, ModifiedDate = d.ModifiedDate, Contactor = d.Contactor, ContactorPhone = d.ContactorPhone, ContactorEmail = d.ContactorEmail, ContactorMobile = d.ContactorPhone };

                return queryStore.First();
            }

        }

        public StoreInfo GetStoreByName(string name)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryStore = from d in db.Stores
                                 where d.Status.Equals(1)
                                 where d.Name.Equals(name)
                                 select new StoreInfo { Id = d.Id, Name = d.Name, Status = d.Status, Address = d.Address, TaxCode = d.TaxCode, Mobile = d.Mobile, Phone = d.Phone, Description = d.Description, CreatedBy = d.CreatedBy, CreatedDate = d.CreatedDate, ModifiedBy = d.ModifiedBy, ModifiedDate = d.ModifiedDate, Contactor = d.Contactor, ContactorPhone = d.ContactorPhone, ContactorEmail = d.ContactorEmail, ContactorMobile = d.ContactorPhone };

                return queryStore.First();
            }

        }

        public IList<StoreInfo> ListStore()
        {
            using (var db = new QLKDSDataContext())
            {
                var queryStore = from d in db.Stores
                                     select new StoreInfo { Id = d.Id, Name = d.Name, Status = d.Status, Address = d.Address, TaxCode = d.TaxCode, Mobile = d.Mobile, Phone = d.Phone, Description = d.Description, CreatedBy = d.CreatedBy, CreatedDate = d.CreatedDate, ModifiedBy = d.ModifiedBy, ModifiedDate = d.ModifiedDate, Contactor = d.Contactor, ContactorPhone = d.ContactorPhone, ContactorEmail = d.ContactorEmail, ContactorMobile = d.ContactorPhone };

                return queryStore.ToList();
            }
        }

        public void CreateStore(Store customer)
        {
            using (var db = new QLKDSDataContext())
            {
                db.Stores.InsertOnSubmit(customer);

                db.SubmitChanges();

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="customer"></param>
        public void UpdateStore(Store customer)
        {
            using (var db = new QLKDSDataContext())
            {
                var _customer = db.Stores.Single(d => d.Id.Equals(customer.Id));


                _customer.Name = customer.Name;
                _customer.Address = customer.Address;
                _customer.Mobile = customer.Mobile;
                _customer.Phone = customer.Phone;
                _customer.Status = customer.Status;
                _customer.TaxCode = customer.TaxCode;
                _customer.Description = customer.Description;
                _customer.CreatedBy = customer.CreatedBy;
                _customer.CreatedDate = customer.CreatedDate;
                _customer.ModifiedBy = customer.ModifiedBy;
                _customer.ModifiedDate = customer.ModifiedDate;
                _customer.Contactor = customer.Contactor;
                _customer.ContactorEmail = customer.ContactorEmail;
                _customer.ContactorMobile = customer.ContactorMobile;
                _customer.ContactorPhone = customer.ContactorPhone;
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteStore(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _customer = db.Stores.Single(d => d.Id.Equals(id));

                _customer.Status = 0;

                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void RemoveStore(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _customer = db.Stores.Single(d => d.Id.Equals(id));

                db.Stores.DeleteOnSubmit(_customer);
                db.SubmitChanges();
            }
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public bool HasMobileExisted(string mobile)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Stores
                            where d.Mobile.Equals(mobile)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasMobileDuplicated(string mobile, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Stores
                            where d.Mobile.Equals(mobile)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

    }
}
